package com.socket.config.webSocket;

import com.socket.utils.LogUtil;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.ChannelInterceptor;

/**
 * 客户端往服务端发送消息的拦截器
 */
public class MyChannelInterceptor implements ChannelInterceptor {

    @Override
    public Message<?> preSend(Message<?> message, MessageChannel channel) {
        StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
        StompCommand command = accessor.getCommand();
        LogUtil.warn(accessor.getDetailedLogMessage(message.getPayload()));
        if (command == null) {
            //发送心跳PING/PONG时，command为null
        } else if (StompCommand.CONNECT.name().equals(command.toString())) {
//            SimpMessageType messageType = command.getMessageType();
            LogUtil.info(accessor.getDetailedLogMessage(message.getPayload()));
        } else if (StompCommand.DISCONNECT.name().equals(command.toString())) {
            LogUtil.info(accessor.getDetailedLogMessage(message.getPayload()));
        }
        return message;
    }
}